/*-------------------------------------------------------------------------

***************** Modification History ***********************************
   @doc
   @module gds.c - Miscellaneous global routines and structure |
   @end
   
$Log: /SoftPay/SPK2SIM/Source/Common/gds.c $ 
**************************************************************************

   Copyright (C) 1999 by VeriFone, Inc.

   All rights reserved.  No part of this software may be reproduced,
   transmitted, transcribed, stored in a retrieval system, or translated
   into any language or computer language, in any form or by any means,
   electronic, mechanical, magnetic, optical, chemical, manual or otherwise,
   without the prior written permission of VeriFone, Inc.

---------------------------------------------------------------------------*/

#include <string.h>
#include <svc.h>
#include <battery.h>
#include <acldev.h>
#include <aclconio.h>

#include <project.h>

/**/#define DEBUG_GDS/**/
#ifdef DEBUG_GDS
#define DEBUG_95
#endif

#define VSGDS_C

#include <define.h>
#include <gds.h>
#include <tct.h>
#include <clerk.h>
#include <logo.h>
#ifdef MESSENGER_DEFINED
#include <msngr.h>
#endif
#include <spdebug.h>

GDS srGDS;

#define OMNI_3300_ID    "Q7"
#define OMNI_3350_ID    "Q3"
#define OMNI_3600_ID    "Q8"
#define OMNI_3750_ID    "Q5"
#define OMNI_5150_ID    "QA"

void vdInitGDS(void) {
    int inRetVal;
    char szDataBuf[50];


    srGDS.lnErrorMessageIndex = 0L;

    srGDS.inCheckReaderHandle = VS_ERR;
    srGDS.inChkrdrPort = VS_ERR;
    srGDS.fCheckSwipeFromIdle = VS_FALSE;

    srGDS.fResetIdleTimer = VS_TRUE;
    srGDS.ulIdleTimerCount = set_itimeout(NO_CLOCK_HANDLE 1000, TM_SECONDS);

    // Get Current DOW
    memset(szDataBuf, 0, 50);
    SVC_CLOCK(GET_CLOCK, szDataBuf, COMPLETE_DATE_AND_TIME_SIZE);
    srGDS.shDoW = szDataBuf[14] - '0';

    srGDS.fBatteryOperated = VS_FALSE;
    srGDS.fWirelessModem = VS_FALSE;
    srGDS.fWirelessActive = VS_FALSE;
    srGDS.fHandHeld = VS_FALSE;
    srGDS.fLandLine = VS_TRUE;
    srGDS.fTDK_MODEM = VS_FALSE;
    srGDS.fSoftFormFeedKey = VS_FALSE;
    srGDS.fExternalEvent = VS_FALSE;

    memset(szDataBuf, 0, 50);
    SVC_INFO_EPROM(szDataBuf);
    //  clrscr();printf("%s", szDataBuf); get_char();

    pdebug(("SVC_INFO_EPROM = %s", szDataBuf));
    if (strstr(szDataBuf, OMNI_3600_ID) != (char *) NULL) {
        pdebug(("OMNI_3600_ID"));

        srGDS.srBatteryStatus.lnMaxValue = get_battery_value(FULLCHARGE);
        srGDS.fBatteryOperated = VS_TRUE;
        srGDS.fWirelessModem = VS_TRUE;
        srGDS.fHandHeld = VS_TRUE;
        srGDS.fSoftFormFeedKey = VS_TRUE;

        vdGetBatteryStatus();
        pdebug(("srBatteryStatus.lnRawValue=%ld", srGDS.srBatteryStatus.lnRawValue));
        pdebug(("srBatteryStatus.lnMaxValue=%ld", srGDS.srBatteryStatus.lnMaxValue));
        pdebug(("srBatteryStatus.shPercentage=%d", srGDS.srBatteryStatus.shPercentage));
        pdebug(("srBatteryStatus.shGraphicIndex=%d", srGDS.srBatteryStatus.shGraphicIndex));

        srGDS.srWirelessSignalStatus.lnRawValue = 0L;
        srGDS.srWirelessSignalStatus.lnMaxValue = 125L;
        pdebug(("srWirelessSignalStatus.lnMaxValue=%ld", srGDS.srWirelessSignalStatus.lnMaxValue));
        srGDS.srWirelessSignalStatus.shPercentage = 0;
        srGDS.srWirelessSignalStatus.shGraphicIndex = 0;
        strcpy(srGDS.modelNo, "OMNI3600");
    } else if (strstr(szDataBuf, OMNI_3750_ID) != (char *) NULL) {
        strcpy(srGDS.modelNo, "OMNI3750");
        pdebug(("OMNI_3750_ID"));

	 inRetVal = SVC_INFO_MOD_ID();
	 pdebug(("3750 SVC_INFO_MOD_ID = %d", inRetVal));


        if (inRetVal == 3) {
            srGDS.fTDK_MODEM = VS_TRUE;
            srGDS.fSoftFormFeedKey = VS_TRUE;
            put_env("#MODEM", "TDK-MODEM", 9);
        } else if (inRetVal == 2) {
            srGDS.fTDK_MODEM = VS_FALSE;
            srGDS.fSoftFormFeedKey = VS_TRUE;
        } else {
            srGDS.fTDK_MODEM = VS_TRUE;
            srGDS.fSoftFormFeedKey = VS_TRUE;
            put_env("#MODEM", "TDK-MODEM", 9);  
            /*SVC_INFO_MOD_ID works for OS 13A0 and above. in case the OS is less than 13A0 
                    it should able to run as old TDK modem terminals. */
        }
    } else if (strstr(szDataBuf, OMNI_3350_ID) != (char *) NULL) {
        strcpy(srGDS.modelNo, "OMNI3350");
        pdebug(("OMNI_3350_ID"));
    } else if (strstr(szDataBuf, OMNI_3300_ID) != (char *) NULL) {
        strcpy(srGDS.modelNo, "OMNI3300");
        pdebug(("OMNI_3300_ID"));
    } else if(strstr(szDataBuf, OMNI_5150_ID) != (char *) NULL) {
        strcpy(srGDS.modelNo, "OMNI5150");
        pdebug(("OMNI_5150_ID"));

 	 inRetVal = SVC_INFO_MOD_ID();
	 pdebug(("5150 SVC_INFO_MOD_ID = %d", inRetVal));
    }	
    /* LeeChy - 09092005: START
    	Added another condition for Vx610
    */
    else if(strstr(szDataBuf, "QB") != (char *) NULL) {
    	 pdebug(("VX610"));
    	 srGDS.srBatteryStatus.lnMaxValue = get_battery_value(FULLCHARGE);
        srGDS.fBatteryOperated = VS_TRUE;
        srGDS.ulIdleTimerCount = set_itimeout(NO_CLOCK_HANDLE inGetIdleTimeOut(), TM_SECONDS);
  	}
  	/* LeeChyi - END */
    else {
        pdebug(("UNIDENTIFIED TERMINAL TYPE ERROR!!"));
    }

#ifdef MESSENGER_DEFINED    
    vdGetMessengerFileData();
#endif
}

long lnGGErrorMessage(void) {
    return (srGDS.lnErrorMessageIndex);
}

void vdSGErrorMessage(long lnErrorMessage) {
    srGDS.lnErrorMessageIndex = lnErrorMessage;
}
